package team.bluepen.supermarket.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import team.bluepen.supermarket.common.HttpResponseEntity;
import team.bluepen.supermarket.data.entity.ProductIdNamesAmountVo;
import team.bluepen.supermarket.data.entity.ProductIdStatusAmount;
import team.bluepen.supermarket.data.entity.ProductNameAmount;
import team.bluepen.supermarket.data.entity.ProductNameStatusAmount;
import team.bluepen.supermarket.data.entity.ProductStatusAmount;
import team.bluepen.supermarket.service.ProductIdAmountRecvService;
import team.bluepen.supermarket.service.ProductIdStatusAmountRecvService;
import team.bluepen.supermarket.service.ProductNameReceiveService;
import team.bluepen.supermarket.service.ProductNameStatusAmountCountRecvService;
import team.bluepen.supermarket.service.ProductStatusRecvService;
import team.bluepen.supermarket.service.calc.ProductRddCountService;

import java.util.Collection;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author RollW
 */
@RestController
@RequestMapping("/api/product")
public class ProductStateController {
    private final ProductStatusRecvService productStatusRecvService;
    private final ProductNameReceiveService productNameReceiveService;
    private final ProductNameStatusAmountCountRecvService productNameStatusAmountCountRecvService;
    private final ProductRddCountService productRddCountService;
    private final ProductIdAmountRecvService productIdAmountRecvService;
    private final ProductIdStatusAmountRecvService productIdStatusAmountRecvService;

    public ProductStateController(ProductStatusRecvService productStatusRecvService,
                                  ProductNameReceiveService productNameReceiveService,
                                  ProductNameStatusAmountCountRecvService productNameStatusAmountCountRecvService,
                                  ProductRddCountService productRddCountService,
                                  ProductIdAmountRecvService productIdAmountRecvService,
                                  ProductIdStatusAmountRecvService productIdStatusAmountRecvService) {
        this.productStatusRecvService = productStatusRecvService;
        this.productNameReceiveService = productNameReceiveService;
        this.productNameStatusAmountCountRecvService = productNameStatusAmountCountRecvService;
        this.productRddCountService = productRddCountService;
        this.productIdAmountRecvService = productIdAmountRecvService;
        this.productIdStatusAmountRecvService = productIdStatusAmountRecvService;
    }

    @GetMapping("/status/amounts")
    public HttpResponseEntity<Collection<ProductStatusAmount>> getStatusAmounts() {
        return HttpResponseEntity.success(
                productStatusRecvService.getStatusAmount()
        );
    }

    @GetMapping("/amounts")
    public HttpResponseEntity<Map<String, Integer>> getProductAmounts() {
        return HttpResponseEntity.success(
                productNameReceiveService.getProductNameAmountMap()
        );
    }

    @GetMapping("/name/amounts")
    public HttpResponseEntity<Collection<ProductNameAmount>> getProductAmountsCollection() {
        return HttpResponseEntity.success(
                productNameReceiveService.getAmounts()
        );
    }

    @GetMapping("/name/status/amounts")
    public HttpResponseEntity<Collection<ProductNameStatusAmount>> statusAmounts() {
        return HttpResponseEntity.success(
                productNameStatusAmountCountRecvService.getAmount()
        );
    }

    @GetMapping("/id/amounts")
    public HttpResponseEntity<Map<Long, Integer>> getProductIdAmounts() {
        return HttpResponseEntity.success(
                productIdAmountRecvService.getProductIdAmount()
        );
    }

    @GetMapping("/id/name/amounts")
    public HttpResponseEntity<Collection<ProductIdNamesAmountVo>> getIdNamesAmount() {
        return HttpResponseEntity.success(
                productRddCountService.getAmountCollection()
                        .stream().map(ProductIdNamesAmountVo::from)
                        .collect(Collectors.toList())
        );
    }

    @GetMapping("/id/name/i/amounts")
    public HttpResponseEntity<ProductIdNamesAmountVo> getIdNamesAmount(@RequestParam long id) {
        return HttpResponseEntity.success(
                ProductIdNamesAmountVo.from(productRddCountService.getById(id))
        );
    }

    @GetMapping("/id/status/amounts")
    public HttpResponseEntity<Collection<ProductIdStatusAmount>> getIdStatusAmount() {
        return HttpResponseEntity.success(
                productIdStatusAmountRecvService.getAmounts()
        );
    }
}
